package com.apestech.api.workflow.extend;

import com.apestech.api.workflow.modul.CounterSignModul;
import com.apestech.framework.json.JSON;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;

/**
 * 会签，选择人员允许选择一人及以上
 *
 * @author hup
 * @create 2018-07-12-15:37
 */
public class CounterSign {

    private ArrayList<CounterSignModul> userlist;
    private String signOrder="";
    private int size;
    private int finish;
    private String personId;
    private String personName;

    public CounterSign(){
        this.signOrder="";
        this.finish=0;
        this.size=-1;
    }

    public CounterSign(JSONArray user) throws Exception {
        if(user==null){
            throw new Exception("会签必须选择人员列表");
        }
        personId="";
        personName="";
        userlist=new ArrayList<CounterSignModul>();
        int len=user.size();
        for(int i=0; i<len; i++){
            JSONObject m=user.getJSONObject(i);
            if(m!=null){
                if(!"".equals(personId)){
                    personId=personId+",";
                }
                personId=personId+m.getString("userId");
                if(!"".equals(personName)){
                    personName=personName+",";
                }
                personName=personName+m.getString("userName");
                userlist.add(new CounterSignModul(m.getString("userId"), m.getString("userName")));
            }
        }
        this.signOrder="";
        this.finish=0;
        this.size=this.userlist.size();
    }

    public String getUserlistStr() throws Exception {
        return JSON.toJSONString(userlist);
    }

    public void setUserlistStr(String text) throws Exception {
        userlist=new ArrayList<CounterSignModul>();
        JSONArray list=JSON.toJSONArray(text);
        size=list.size();
        for(int i=0; i<size; i++){
            userlist.add(new CounterSignModul(new JSONObject((Map<String, Object>) list.get(i))));
        }
    }

    public ArrayList<CounterSignModul> getUserlist() {
        return userlist;
    }

    public void setUserlist(ArrayList<CounterSignModul> userlist) {
        this.userlist = userlist;
    }

    public String getSignOrder() {
        return signOrder;
    }

    public void setSignOrder(String signOrder) {
        if(signOrder==null) signOrder="";
        this.signOrder = signOrder;
    }

    public int getFinish() {
        return finish;
    }

    public void setFinish(int finish) {
        this.finish = finish;
    }

    public String getPersonId() {
        return personId;
    }

    public String getPersonName() {
        return personName;
    }

    public void exec(String user) throws Exception {
        if(finish==size){
            throw new Exception("会签已经结束！");
        }
        boolean flag=false;
        for(int i=0; i<size; i++){
            if((","+signOrder+",").indexOf(","+i+",")<0){
                if(user.equals(userlist.get(i).getUserId())){
                    if(!("").equals(signOrder)){
                        signOrder=signOrder+",";
                    }
                    signOrder=signOrder+i;
                    finish++;
                    flag=true;
                    break;
                }
            }
        }
        if(!flag){
            throw new Exception("请选择合法的会签人员"+getUnFinishUser().toString()+"！");
        }
    }

    public boolean isFinish(){
        return (finish==size);
    }

    public ArrayList<String> getFinishUser(){
        ArrayList<String> list=new ArrayList<String>();
        for(int i=0; i<size; i++){
            if((","+signOrder+",").indexOf(","+i+",")>=0){
                list.add(userlist.get(i).getUserId());
            }
        }
        return list;
    }

    public ArrayList<String> getUnFinishUser(){
        ArrayList<String> list=new ArrayList<String>();
        for(int i=0; i<size; i++){
            if((","+signOrder+",").indexOf(","+i+",")<0){
                list.add(userlist.get(i).getUserId());
            }
        }
        return list;
    }

}
